﻿<ServiceContract(CallbackContract:=GetType(IClient))> _
Public Interface IServer

    <OperationContract(isoneway:=True)> _
    Sub Hello()

    <OperationContract(isoneway:=True)> _
    Sub DoLogin(ByVal Username As String, ByVal Password As String)

    <OperationContract(isoneway:=True)> _
    Sub CreateUser(ByVal Username As String, ByVal Password As String)

    <OperationContract(isoneway:=True)> _
    Sub ChangePassword(ByVal Username As String, ByVal OldPassword As String, ByVal NewPassword As String)

    <OperationContract(isoneway:=True)> _
    Sub CreateTeam(ByVal Teamname As String, ByVal User As String)

    <OperationContract(isoneway:=True)> _
    Sub JoinTeam(ByVal Teamname As String, ByVal User As String)

    <OperationContract(isoneway:=True)> _
    Sub GetGames()

    <OperationContract(isoneway:=True)> _
    Sub CreateGame(ByVal id As Integer, ByVal Name As String)

    <OperationContract(isoneway:=True)> _
    Sub GetUserDetails(ByVal Username As String)

    <OperationContract(isoneway:=True)> _
    Sub UpdateUserScores()

    <OperationContract(isoneway:=True)> _
    Sub UpdateTeamScores()

    <OperationContract(isoneway:=True)> _
    Sub RegisterClient(ByVal GameID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub RegisterHost(ByVal GameID As Integer, ByVal Name As String, ByVal QuizFile As String)

    <OperationContract(isoneway:=True)> _
    Sub RegisterUser(ByVal GameID As Integer, ByVal Name As String)

    <OperationContract(isoneway:=True)> _
    Sub UnregisterClient(ByVal GameID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub UnregisterUser(ByVal GameID As Integer, ByVal ID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub GetGameDetails(ByVal GameID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub ReadyToStart(ByVal GameID As Integer, ByVal ID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub MultichoiceAnswer(ByVal GameID As Integer, ByVal Answer As Answer, ByVal ID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub InputAnswer(ByVal GameID As Integer, ByVal Answer As String, ByVal ID As Integer)

    <OperationContract(Isoneway:=True)> _
    Sub OutOfTime(ByVal GameID As Integer, ByVal ID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub GetQuizFiles()

    <OperationContract(isoneway:=True)> _
    Sub UploadQuizFile(ByVal QuizFile As String, ByVal Name As String, ByVal PartNo As Integer, ByVal TotalParts As Integer)

    <OperationContract(isoneway:=True)> _
    Sub StartQuiz(ByVal GameID As Integer)

    <OperationContract(Isoneway:=True)> _
    Sub GetHaveHost(ByVal GameID As Integer)

    <OperationContract(Isoneway:=True)> _
    Sub Goodbye()

    <OperationContract(Isoneway:=True)> _
    Sub IncorrectPassword()

    <OperationContract(Isoneway:=True)> _
    Sub SetUserMultipler(ByVal Username As String, ByVal Multiplier As Double)
End Interface

Public Interface IClient

    <OperationContract(isoneway:=True)> _
    Sub OnHello(ByVal SingleGame As Boolean, ByVal ServerMessages As List(Of ServerMessage), ByVal ServerRequiresPassword As Boolean)

    <OperationContract(Isoneway:=True)> _
    Sub OnLoginSuccess()

    <OperationContract(Isoneway:=True)> _
    Sub OnLoginFail()

    <OperationContract(isoneway:=True)> _
    Sub OnCreateUserFail(ByVal Reason As String)

    <OperationContract(isoneway:=True)> _
    Sub OnChangePassword(ByVal Success As Boolean, Optional ByVal Exception As Exception = Nothing)

    <OperationContract(isoneway:=True)> _
    Sub OnFailUpload(ByVal Exception As Exception)

    <OperationContract(isoneway:=True)> _
    Sub OnGetGames(ByVal Games As Dictionary(Of String, Integer))

    <OperationContract(isoneway:=True)> _
    Sub OnGetUserDetails(ByVal Score As Integer, ByVal TeamName As String, ByVal TimeMultiplier As Double)

    <OperationContract(isoneway:=True)> _
    Sub OnUpdateUserScores(ByVal Scores As SortedList(Of String, Long))

    <OperationContract(isoneway:=True)> _
    Sub OnUpdateTeamScores(ByVal Scores As SortedList(Of String, Long))

    <OperationContract(isoneway:=True)> _
    Sub OnRegisterClient(ByVal GameName As String)

    <OperationContract(isoneway:=True)> _
    Sub OnFailRegisterClient(ByVal Reason As String)

    <OperationContract(isoneway:=True)> _
    Sub OnGetGameDetails(ByVal Details As GameDetails)

    <OperationContract(isoneway:=True)> _
    Sub SendQuestion(ByVal Question As Question)

    <OperationContract(isoneway:=True)> _
    Sub Start()

    <OperationContract(isoneway:=True)> _
    Sub UpdateGameScores(ByVal Scores As SortedList(Of String, Long))

    <OperationContract(isoneway:=True)> _
    Sub SendStatus(ByVal Status As String, Optional ByVal Detail As String = "")

    <OperationContract(isoneway:=True)> _
    Sub OnRegisterHost(ByVal ID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub OnRegisterUser(ByVal ID As Integer)

    <OperationContract(isoneway:=True)> _
    Sub OnCorrectAnswer(ByVal Explanation As String)

    <OperationContract(isoneway:=True)> _
    Sub OnIncorrectAnswer(ByVal CorrectAnswer As String, ByVal Explanation As String)

    <OperationContract(isoneway:=True)> _
    Sub HaveHost(ByVal HaveHost As Boolean)

    <OperationContract(isoneway:=True)> _
    Sub OnGetQuizFiles(ByVal Files() As String)

    <OperationContract(isoneway:=True)> _
    Sub EndOfQuiz(ByVal UserScore As Integer, ByVal TotalQuestions As Integer)

    <OperationContract(isoneway:=True)> _
    Sub OnIncorrectServerPassword()
End Interface

<DataContract()> _
Public Class User

    Dim _CallBack As IClient
    Dim _Channel As Channels.IChannel
    Dim _Name As String
    Dim _Score As Integer
    Dim _ID As Integer
    Dim _IsHost As Boolean
    Dim _TimeMultiplier As Double

    Sub New(ByVal Callback As IClient, ByVal Channel As Channels.IChannel, ByVal Name As String, ByVal ID As Integer, ByVal IsHost As Boolean, ByVal TimeMultiplier As Double)
        _Channel = Channel
        _CallBack = Callback
        _Name = Name
        _Score = 0
        _ID = ID
        _IsHost = IsHost
        _TimeMultiplier = TimeMultiplier
    End Sub

    <DataMember()> _
    Public Property CallBack As IClient
        Get
            Return _CallBack
        End Get
        Set(ByVal value As IClient)
            _CallBack = value
        End Set
    End Property
    Public Property Channel As Channels.IChannel
        Get
            Return _Channel
        End Get
        Set(ByVal value As Channels.IChannel)
            _Channel = value
        End Set
    End Property

    <DataMember()> _
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
    <DataMember()> _
    Public Property Score As Integer
        Get
            Return _Score
        End Get
        Set(ByVal value As Integer)
            _Score = value
        End Set
    End Property
    <DataMember()> _
    Public Property ID As Integer
        Get
            Return _ID
        End Get
        Set(ByVal value As Integer)
            _ID = value
        End Set
    End Property
    <DataMember()> _
    Public Property IsHost As Boolean
        Get
            Return _IsHost
        End Get
        Set(ByVal value As Boolean)
            _IsHost = value
        End Set
    End Property
    <DataMember()> _
    Public Property TimeMultiplier
        Get
            Return _TimeMultiplier
        End Get
        Set(ByVal value)
            _TimeMultiplier = value
        End Set
    End Property
End Class

<DataContract()> _
Public Enum Answer
    <EnumMember()> A
    <EnumMember()> B
    <EnumMember()> C
    <EnumMember()> D
End Enum

<DataContract()> _
Public Structure Question
    <DataMember()> Public QuestionID As Integer
    <DataMember()> Public QuestionText As String
    <DataMember()> Public A As String
    <DataMember()> Public B As String
    <DataMember()> Public C As String
    <DataMember()> Public D As String
    <DataMember()> Public CorrectAnswer As String
    <DataMember()> Public TimeLimit As Integer
    <DataMember()> Public IsMultichoice As Boolean
    <DataMember()> Public Image As String
End Structure

<DataContract()> _
Public Class ServerMessage

    <DataMember()> _
    Dim _Title As String
    <DataMember()> _
    Dim _Message As String
    <DataMember()> _
    Dim _Icon() As Byte


    Public Property Title As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property

    Public Property Message As String
        Get
            Return _Message
        End Get
        Set(ByVal value As String)
            _Message = value
        End Set
    End Property

    Public Property Icon As Drawing.Image
        Get
            Dim ms As New IO.MemoryStream
            ms.Write(_Icon, 0, _Icon.Length)
            ms.Position = 0
            Return Drawing.Image.FromStream(ms)
        End Get
        Set(ByVal value As Drawing.Image)
            If value Is Nothing Then Exit Property
            Dim ms As New IO.MemoryStream
            value.Save(ms, Drawing.Imaging.ImageFormat.Png)
            Dim Buffer(ms.Length - 1) As Byte
            ms.Position = 0
            ms.Read(Buffer, 0, ms.Length - 1)
            _Icon = Buffer
        End Set
    End Property
End Class

<DataContract()> _
Public Class GameDetails

    Dim _Image As String
    Dim _Name As String
    Dim _Description As String
    Dim _TotalQuestions As Integer
    Dim _Author As String

    Sub New(ByVal Image As String, ByVal Name As String, ByVal Description As String, ByVal TotalQuestions As Integer, ByVal Author As String)
        _Image = Image
        _Name = Name
        _Description = Description
        _TotalQuestions = TotalQuestions
        _Author = Author
    End Sub

    Sub New()

    End Sub

    <DataMember()> _
    Public Property Image As String
        Get
            Return _Image
        End Get
        Set(ByVal value As String)
            _Image = value
        End Set
    End Property

    <DataMember()> _
    Public Property Name As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    <DataMember()> _
    Public Property Description As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property

    <DataMember()> _
    Public Property TotalQuestions As Integer
        Get
            Return _TotalQuestions
        End Get
        Set(ByVal value As Integer)
            _TotalQuestions = value
        End Set
    End Property

    <DataMember()> _
    Public Property Author As String
        Get
            Return _Author
        End Get
        Set(ByVal value As String)
            _Author = value
        End Set
    End Property
End Class

